package com.ruoyi.lhb.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lhb.domain.Ordertime;
import com.ruoyi.lhb.util.Util;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.whc.domain.Checkpoint;
import com.ruoyi.whc.service.ICheckpointService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lhb.service.IOrdertimeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排班Controller
 *
 * @author lhb
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/lhb/ordertime")
public class OrdertimeController extends BaseController
{
    @Autowired
    private IOrdertimeService ordertimeService;

    @Autowired
    private ICheckpointService checkpointService;



    /**
     * 查询排班列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ordertime ordertime)
    {
//        System.out.println(SecurityUtils.getUserId());
//        List<SysUserRole> userRole = ordertimeService.selectRoleByuserid(SecurityUtils.getUserId());
//        System.out.println(userRole.get(0).getRoleId()+"================");
//        if (userRole.get(0).getRoleId() == 100){
//            ordertime.setBid(SecurityUtils.getUserId());
//        }
        System.out.println("进入list了");
        ordertime.setBid(SecurityUtils.getUserId());
        startPage();
        List<Ordertime> list = ordertimeService.selectOrdertimeList(ordertime);
        return getDataTable(list);
    }

    /**
     * 查询排班列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:list1')")
    @GetMapping("/list1")
    public TableDataInfo list1(Ordertime ordertime)
    {
        System.out.println("进来这里了list1了");
//        System.out.println(SecurityUtils.getUserId());
//        List<SysUserRole> userRole = ordertimeService.selectRoleByuserid(SecurityUtils.getUserId());
//        System.out.println(userRole.get(0).getRoleId()+"================");
//        if (userRole.get(0).getRoleId() == 100){
//            ordertime.setBid(SecurityUtils.getUserId());
//        }
        startPage();
        List<Ordertime> list = ordertimeService.selectOrdertimeList(ordertime);
        return getDataTable(list);
    }

    /**
     * 导出排班列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:export')")
    @Log(title = "排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ordertime ordertime)
    {
        List<Ordertime> list = ordertimeService.selectOrdertimeList(ordertime);
        ExcelUtil<Ordertime> util = new ExcelUtil<Ordertime>(Ordertime.class);
        util.exportExcel(response, list, "排班数据");
    }

    /**
     * 获取排班详细信息
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:query')")
    @GetMapping(value = "/{otid}")
    public AjaxResult getInfo(@PathVariable("otid") Long otid)
    {
        return AjaxResult.success(ordertimeService.selectOrdertimeByOtid(otid));
    }

    /**
     * 新增排班
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:add')")
    @Log(title = "排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ordertime ordertime)
    {
        System.out.println(ordertime.getDate());
        ordertimeService.deleteOrdertimeBydate(ordertime.getDate());
        System.out.println(ordertime.getCpid());
        Checkpoint checkpoint = checkpointService.selectCheckpointByCpid(ordertime.getCpid());
        String datetime = checkpoint.getStartdate() + "-" + checkpoint.getEndtime();
        ordertime.setTime(datetime);
        return toAjax(ordertimeService.insertOrdertime(ordertime));
    }

    /**
     * 修改排班
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:edit')")
    @Log(title = "排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ordertime ordertime)
    {
        return toAjax(ordertimeService.updateOrdertime(ordertime));
    }

    /**
     * 删除排班
     */
    @PreAuthorize("@ss.hasPermi('lhb:ordertime:remove')")
    @Log(title = "排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{otids}")
    public AjaxResult remove(@PathVariable Long[] otids)
    {
        return toAjax(ordertimeService.deleteOrdertimeByOtids(otids));
    }
}
